package graduate.wangyaqi.Service;

import  java.util.Properties;
import java.util.Random;

import  org.springframework.mail.SimpleMailMessage;
import  org.springframework.mail.javamail.JavaMailSenderImpl;


public  class MailService {

    public  static String SendEmail(String email){
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost("smtp.163.com");

        // 建立邮件消息
        SimpleMailMessage mailMessage  =   new  SimpleMailMessage();
        // 设置收件人，寄件人 用数组发送多个邮件
        // String[] array = new String[]    {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo( email);
        mailMessage.setFrom( "m18408078908@163.com" );
        mailMessage.setSubject( "基于云平台的C语言微课程学习系统" );
        String num = RandNum();
        mailMessage.setText( "您的验证码："+ num);

        senderImpl.setUsername("m18408078908@163.com") ;  //  根据自己的情况,设置username
        senderImpl.setPassword("qq123123");  //  根据自己的情况, 设置password

        Properties prop =  new  Properties() ;
        prop.put("mail.smtp.auth",  "true") ;  //  将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put( "mail.smtp.timeout", "25000") ;
        prop.setProperty("mail.smtp.ssl.enable", "true");
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件
        senderImpl.send(mailMessage);
        return num;
    }

    public static String RandNum(){
        String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了
        Random rand = new Random();
        StringBuffer flag = new StringBuffer();
        for (int j = 0; j < 6; j++)
        {
            flag.append(sources.charAt(rand.nextInt(9)) + "");
        }
        return flag.toString();
    }
}
